package com.example.myapplication.Penson;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Config.Content;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.RunOnUiThreadTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.Config.User;
import com.example.myapplication.R;

import org.json.JSONException;
import org.json.JSONObject;

public class LonigActivity extends AppCompatActivity  {

    private TextView penter_exit;
    private EditText ed_pwd;
    private EditText ed_user;
    private String  TAG  = "TAG";
    private ImageView but_lawyerList;
    private SharedPreferences sharedPreferences;
    private String pwdd;
    private SharedPreferences login;
    private SharedPreferences.Editor edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lonig);
        initview();
        img_bloak();
        login();
    }

    private void img_bloak() {
        but_lawyerList.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LonigActivity.this.finish();
                    }
                }
        );
    }

    private void login() {
        penter_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入的账号密码
                String user = ed_user.getText().toString();
                String pwd  = ed_pwd.getText().toString();

                if(!"".equals(user.trim()) && !"".equals(pwd)
                    && !TextUtils.isEmpty(user) && !TextUtils.isEmpty(pwd)){
                    gettokenandstatus(user,pwd);
                }else {
                    Toast.makeText(LonigActivity.this,"请输入账号密码",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void gettokenandstatus(String user, String pwd) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username",user);
            jsonObject.put("password",pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String JSOn = jsonObject.toString();
        //判断token和状态码是否未登录成功
        OKHttpTool.doNetRequest("prod-api/api/login",
                OKHttpTool.POST,
                Content.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RunOnUiThreadTool.run(LonigActivity.this, new RunOnUiThreadTool.RunCallback() {
                            @Override
                            public void set() {
                                Content content = (Content)obj;
                                if(content.getCode() ==200){
                                    Toast.makeText(LonigActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                                    //将账号、密码、token、保存到本地
                                    edit.putString("username",user);
                                    edit.putString("password",pwd);
                                    edit.putString("token",content.getToken());
                                    edit.apply();

                                    OKHttpTool.TOKEN = content.getToken();
                                    TokenBean.token = content.getToken();
                                    LonigActivity.this.finish();
                                }else {
                                    Toast.makeText(LonigActivity.this,"登录失败",Toast.LENGTH_SHORT).show();

                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                },JSOn);
    }



    private void initview() {
        but_lawyerList = findViewById(R.id.but_lawyerList);
        ed_user = findViewById(R.id.ed_user);
        ed_pwd = findViewById(R.id.ed_pwd);
        penter_exit = findViewById(R.id.penter_exit);

        login = getSharedPreferences("login", Context.MODE_PRIVATE);
        edit = login.edit();

        ed_user.setText(User.USERNAME);
        String password = login.getString("password", "");
        ed_pwd.setText(password);
    }
}